using System;

namespace DslCrossModelFramework.ModelRelationship {
    /// <summary>
    /// Defines relationships on model elements which will be used to find appropriate child objects
    /// </summary>
    [AttributeUsage(AttributeTargets.Class, AllowMultiple = true, Inherited = true)]
    public class RelationshipDefinitionAttribute : Attribute {

        /// <summary>
        /// Initializes a new instance of the <see cref="RelationshipDefinitionAttribute"/> class.
        /// </summary>
        /// <param name="relationshipDefinitionType">Type of the relationship definition.</param>
        public RelationshipDefinitionAttribute(Type relationshipDefinitionType) : this(relationshipDefinitionType, string.Empty) {}

        /// <summary>
        /// Initializes a new instance of the <see cref="RelationshipDefinitionAttribute"/> class.
        /// </summary>
        /// <param name="relationshipDefinitionType">Type of the relationship definition.</param>
        /// <param name="scope">The scope.</param>
        public RelationshipDefinitionAttribute(Type relationshipDefinitionType, string scope) {
            if(relationshipDefinitionType.GetInterface(typeof(IModelRelationshipDefinition).Name) == null) {
                throw new InvalidRelationshipDefinitionTypeException(relationshipDefinitionType);
            }
            this.relationshipDefinitionType = relationshipDefinitionType;
            this.scope = scope;
        }

        private Type relationshipDefinitionType;

        /// <summary>
        /// Gets or sets the type of the relationship definition.
        /// </summary>
        /// <value>The type of the relationship definition.</value>
        public Type RelationshipDefinitionType {
            get { return relationshipDefinitionType; }
            set { relationshipDefinitionType = value; }
        }

        private string scope;

        /// <summary>
        /// Gets or sets the scope.
        /// </summary>
        /// <value>The scope.</value>
        public string Scope {
            get { return scope; }
            set { scope = value; }
        }

    }

    public class InvalidRelationshipDefinitionTypeException : Exception {
        public InvalidRelationshipDefinitionTypeException(Type relationshipDefinitionType)
            : base(
                string.Format(
                    DslCrossModelFramework.Resources.Errors.RelationshipDefinitionMustImplementIModelRelationshipDefinition,
                    relationshipDefinitionType.FullName)) {}
    }
}